package com.hmy.cornpestdet.controller;

import com.hmy.cornpestdet.pojo.User;
import com.hmy.cornpestdet.service.AdminUserInfoService;
import com.hmy.cornpestdet.service.UserLoginRegisterService;
import com.hmy.cornpestdet.utils.Result;
import com.hmy.cornpestdet.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class AdminUserInfoController {
    @Autowired
    private AdminUserInfoService adminUserInfoService;

    @GetMapping("/getAllUser")
    public Result getAllUser() {
        List<User>users = adminUserInfoService.getAllUser();
        if(users == null)return Result.Error("获取用户数据失败，请检查网络");
        return Result.Success("获取用户数据成功",users);
    }

    @GetMapping("/getUserByUid")
    public Result getUserByUid(@RequestParam int uid) {
        return adminUserInfoService.getUserByUid(uid);
    }

    @PostMapping("/deleteUserByUid")
    public Result deleteUserByUid(@RequestParam int uid) {
        if(adminUserInfoService.deleteUserByUid(uid) > 0){
            return Result.Success("删除成功", adminUserInfoService.getAllUser());
        }
        return Result.Error("删除失败");
    }

    @PostMapping("/addUser")
    public Result addUser(@RequestBody UserRegisterVo userRegisterVo) {
        return adminUserInfoService.addUser(userRegisterVo);
    }

    @PostMapping("/updateUserByUid")
    public Result updateUserByUid(@RequestParam int uid, @RequestParam String username, @RequestParam String password, @RequestParam String phoneNumber) {
        return adminUserInfoService.updateUserByUid(uid, username, password, phoneNumber);
    }
}
